//
//  StockKLineModel.m
//  K线图自治
//
//  Created by LK-IOS on 2017/12/12.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "StockKLineModel.h"

@interface StockKLineModel()


@end

@implementation StockKLineModel
{
    NSDictionary * _dict;
    NSNumber *Open;//开盘价
    NSNumber *Close;//收盘价
    NSNumber *High;//最高价
    NSNumber *Low;//最低价
    NSNumber *Volume;//成交量
    NSNumber *VolumeValue;//成交额
    NSNumber *priceUpDown;//价格涨跌幅度 ， 为当日最高价与收盘价的差值
    NSNumber *priceUpDownRate;//价格涨跌百分比  为当日最高价与收盘价的差值比上开盘价
    NSNumber *currentPrice;//当前价，K线里面的当前价为最高最低的平均值
    NSString *date;//时间日期
    BOOL isShowDay;//是否显示日期时间
}


-(NSNumber *)Open
{
    return _dict[@"openPrice"];
}
-(NSNumber *)Close
{
    return _dict[@"closePrice"];
}
-(NSNumber *)High
{
    return _dict[@"maxPrice"];
}
-(NSNumber *)Low
{
    return _dict[@"minPrice"];
}
//成交量   手
-(NSNumber *)Volume
{
    return [NSNumber numberWithFloat:[[NSString stringWithFormat:@"%@" , _dict[@"nowVolume"]] floatValue] / 100.0];
}
//总成交额暂时先写死，无数据
-(NSNumber *)VolumeValue
{
    return [NSNumber numberWithFloat:100.0];
}

-(NSNumber *)priceUpDown
{
    return [self calculatePriceChangeWithDict:_dict];
}
-(NSNumber *)priceUpDownRate
{
    return [self calculatePriceChangeRateWithDict:_dict];
}

-(NSNumber *)currentPrice
{
    return [self calculatePriceAverageValueWithDict:_dict];
}

-(NSString *)date
{
    return _dict[@"day"];
}
-(BOOL)isShowDay
{
    return self.showDay.length > 0;
}

-(instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        _dict = dict;
        Close = _dict[@"closePrice"];
        Open = _dict[@"openPrice"];
        High = _dict[@"maxPrice"];
        Low = _dict[@"minPrice"];
        date = _dict[@"day"];
        Volume = [NSNumber numberWithFloat:[[NSString stringWithFormat:@"%@" , _dict[@"nowVolume"]] floatValue] / 100.0];
        VolumeValue = [NSNumber numberWithFloat:100.0];
        priceUpDown = [self calculatePriceChangeWithDict:dict];
        priceUpDownRate = [self calculatePriceChangeRateWithDict:dict];
        currentPrice = [self calculatePriceAverageValueWithDict:dict];
    }
    return self;
}



/**
 计算价格变动值
 */
-(NSNumber *)calculatePriceChangeWithDict:(NSDictionary *)dict
{
    NSNumber *openPrice = dict[@"openPrice"];
    NSNumber *closePrice = dict[@"closePrice"];
    NSNumber *priceChange = [NSNumber numberWithFloat:[closePrice floatValue] - [openPrice floatValue]];
    
    return priceChange;
}

/**
 计算价格变动百分比
 */
-(NSNumber *)calculatePriceChangeRateWithDict:(NSDictionary *)dict
{
    NSNumber *openPrice = dict[@"openPrice"];
    NSNumber *closePrice = dict[@"closePrice"];
    NSNumber *priceRate = [NSNumber numberWithFloat:([closePrice floatValue] - [openPrice floatValue]) / [openPrice floatValue]];
    
    return priceRate;
}

/**
 计算当前价
 */
-(NSNumber *)calculatePriceAverageValueWithDict:(NSDictionary *)dict
{
    NSNumber *highPrice = dict[@"maxPrice"];
    NSNumber *lowPrice = dict[@"minPrice"];
    
    return [NSNumber numberWithFloat:([highPrice floatValue] + [lowPrice floatValue]) * 0.5];
}


@end
